package com.kgc.scd.consumer;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kgc.scd.bean.HotKey;
import com.kgc.scd.bean.NotifySeckill;
import com.kgc.scd.bean.SeckillNotifyResult;
import com.kgc.scd.contant.SecKillMQConstant;
import com.kgc.scd.mapper.NotifySeckillMapper;
import com.kgc.scd.service.sendMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

/**
* @author: HarryXzy
* @date: 2022/6/15 12:29
* @return:
* @description: 秒杀通知消费者
*/
@Slf4j
@RestController
public class SecKillNotifyConsumer {
    @Autowired
    private sendMailService sendMailService;

    @Autowired(required = false)
    private NotifySeckillMapper notifySeckillMapper;

    /**
     * 秒杀活动通知
     */
    @JmsListener(destination = SecKillMQConstant.NOTIFY_USER_SECKILL_P2P)
    public void queueP2P(List<SeckillNotifyResult> getNowNeedNotify){
        // 遍历要发送通知
        for (SeckillNotifyResult seckillNotifyResult : getNowNeedNotify) {
            // 发送通知邮件
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatTime = simpleDateFormat.format(seckillNotifyResult.getSeckillStarttime());
            String text = "尊敬的用户："+seckillNotifyResult.getUserName()+"您好，秒杀活动（"+seckillNotifyResult.getSeckillName()
                    + "）将于"+formatTime+ "开始，建议您提前设置好默认地址，尽快登陆商城！";
            sendMailService.sendEmai("百联商城-秒杀即将开始",text,seckillNotifyResult.getUserEmail());
            NotifySeckill build = new NotifySeckill().builder().id(seckillNotifyResult.getId()).state(1).build();
            notifySeckillMapper.updateById(build);
        }
    }



}
